package com.dasberg.guice;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * Aspect that injects all dependencies for the class that contains
 * the @InjectDependencies annotation.
 * @author mischa
 */
@Aspect
public class DependeciesOnConstructionInjector {
    /**
     * Checks if the constructor has an InjectDependencies annotation present.
     * @param instantiated The Object.
     */
    @Pointcut(" execution(@InjectDependencies *.new(..)) && target(instantiated)")
    public void classToBeInjectedOnInstantiation(Object instantiated) {
    }

    /**
     * Injects the dependencies of the given object.
     * @param instantiated The object.
     */
    @After(" classToBeInjectedOnInstantiation(instantiated) ")
    public void onInstantiation(Object instantiated) {
        StaticInjector.injectDependencies(instantiated);
    }
}